<?php

namespace App\System\Log\Repositories;

use App\System\Base\Helpers\Traits\SinglePattern;
use App\System\Base\Repositories\Repository;
use App\System\Log\Model\LogRepeatedElimination;

class LogRepeatedEliminationRepository extends Repository
{
    use SinglePattern;

    /*
     * 记录列表
     * $getAll=false:获取分页数据 true：获取全部数据
     */
    public function getList($keyword='',$startTime='',$endTime='',$getAll=false)
    {
        if($getAll){
            $data = LogRepeatedElimination::with(['user','order'])
                ->when($keyword,function($query)use($keyword){
                    return $query->whereHas('user',function ($query) use ($keyword){
                        $query->where('name','like','%'.$keyword.'%');
                    });
                })
                ->when($startTime,function ($query)use($startTime){
                    return $query->where('created_at','>=',$startTime);
                })
                ->when($endTime,function ($query)use($endTime){
                    return $query->where('created_at','<=',$endTime.' 23:59:59');
                })
                ->orderBy('created_at', 'desc')
                ->get();
        }else{
            $data = LogRepeatedElimination::with(['user','order'])
                ->when($keyword,function($query)use($keyword){
                    return $query->whereHas('user',function ($query) use ($keyword){
                        $query->where('name','like','%'.$keyword.'%');
                    });
                })
                ->when($startTime,function ($query)use($startTime){
                    return $query->where('created_at','>=',$startTime);
                })
                ->when($endTime,function ($query)use($endTime){
                    return $query->where('created_at','<=',$endTime.' 23:59:59');
                })
                ->orderBy('created_at', 'desc')
                ->paginate();
        }
        return $data;
    }

    /**
     * 新增一条数据
     * @param $data
     */
    public function addData($data)
    {
        $res = LogRepeatedElimination::create($data);
        if(!$res)
        {
            return false;
        }
        return $res->id;
    }

}